/*
 * @(#)UIIndustryServiceImp.java
 * Copyright (c) 2011 TravelSky Technology Ltd. All Right Reserved. 
 */
package com.meetingshow.meeting.uiservice;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.log4j.Logger;

import com.meetingshow.common.base.service.model.QueryResults;
import com.meetingshow.common.base.uiservice.BaseUIServiceImp;
import com.meetingshow.common.base.uiservice.exception.BaseUIServiceException;
import com.meetingshow.common.validator.RemoveValidator;
import com.meetingshow.common.validator.SaveValidator;
import com.meetingshow.common.validator.UpdateValidator;
import com.meetingshow.meeting.dao.model.IndustryPO;
import com.meetingshow.meeting.service.IIndustryService;
import com.meetingshow.meeting.service.model.IndustryQueryParas;
import com.meetingshow.meeting.uiservice.exception.IndustryNotExistException;
import com.meetingshow.meeting.uiservice.model.IndustryQueryParasVO;
import com.meetingshow.meeting.uiservice.model.IndustryVO;

public class UIIndustryServiceImp extends BaseUIServiceImp implements IUIIndustryService {

    private final static Logger logger = Logger.getLogger(UIIndustryServiceImp.class);
    
    private IIndustryService industryService;

    public IIndustryService getIndustryService() {
        return industryService;
    }

    public void setIndustryService(IIndustryService industryService) {
        this.industryService = industryService;
    }

	@Override
	public IndustryVO getIndustryById (java.lang.Integer industryId) throws BaseUIServiceException {
	    if (industryId == null) {
	        this.throwValidaterException("industryId is null.");
	    }
	    
	    IndustryPO industryPO = null;
	    try {
	        industryPO = getIndustryService().getIndustryById(industryId);
	    } catch (Exception e) {
            logger.error(e.getMessage(), e);
            throw new BaseUIServiceException(e);
	    }
	    
	    if (industryPO != null) {
	        return new IndustryVO(industryPO);
	    } else {
	        return null;
	    }
	}

    @Override
    public QueryResults<IndustryVO> queryIndustry(IndustryQueryParasVO industryQueryParasVO) throws BaseUIServiceException {
        
        if (industryQueryParasVO == null) {
            this.throwValidaterException("industryQueryParasVO is null.");
        }
        
        validateThrowEx(industryQueryParasVO);
        
        QueryResults<IndustryVO> industryVOQueryResults = new QueryResults<IndustryVO>();
        List<IndustryVO> industryVOList = new ArrayList<IndustryVO>();
        QueryResults<IndustryPO> industryPOQueryResults = null;
        
        try {
            industryPOQueryResults = getIndustryService().queryIndustry(industryQueryParasVO.toIndustryQueryParas());
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            throw new BaseUIServiceException(e);
        }
        
        if (QueryResults.resultsListNotNull(industryPOQueryResults)) {
            for (IndustryPO industryPO : industryPOQueryResults.getResultList()) {
                if (industryPO != null) {
                    industryVOList.add(new IndustryVO(industryPO));
                }
            }
        }
        
        industryVOQueryResults.setResultList(industryVOList);
        industryVOQueryResults.setFirstResultNo(industryPOQueryResults.getFirstResultNo());
        industryVOQueryResults.setResultsPerGroup(industryPOQueryResults.getResultsPerGroup());
        industryVOQueryResults.setTotalResultsNum(industryPOQueryResults.getTotalResultsNum());
        
        return industryVOQueryResults;
    }

    @Override
    public void removeIndustry(IndustryVO industryVO)
            throws BaseUIServiceException {
        
        if (industryVO == null) {
            this.throwValidaterException("industryVO is null");
        }
        
        validateThrowEx(industryVO, RemoveValidator.class);
        
        IndustryQueryParas industryQueryParas = new IndustryQueryParas();
        industryQueryParas.setIndustryId_e(String.valueOf(industryVO.getIndustryId()));
        QueryResults<IndustryPO> industryQueryResults = null;
        try {
            industryQueryResults = getIndustryService().queryIndustry(industryQueryParas);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            throw new BaseUIServiceException(e);
        }
        
        if (!QueryResults.resultsListNotNull(industryQueryResults)) {
            throw new IndustryNotExistException();
        }
        
        IndustryPO industryPO = industryQueryResults.getResultList().get(0);
        try {
            getIndustryService().removeIndustry(industryPO);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            throw new BaseUIServiceException(e);
        }
    }
    
    @Override
    public java.lang.Integer saveIndustry(IndustryVO industryVO)
            throws BaseUIServiceException {
        if (industryVO == null) {
            this.throwValidaterException("industryVO is null");
        }
        industryVO.setCreateTime(new Date());
        validateThrowEx(industryVO, SaveValidator.class);
        
        IndustryPO industryPO = industryVO.toIndustryPO();
        try {
            getIndustryService().saveIndustry(industryPO);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            throw new BaseUIServiceException(e);
        }
        
        return industryPO.getIndustryId();
    }

    @Override
    public void updateIndustry(IndustryVO industryVO)
            throws BaseUIServiceException {
        if (industryVO == null) {
            this.throwValidaterException("industryVO is null");
        }
        
        validateThrowEx(industryVO, UpdateValidator.class);
        
		IndustryQueryParas industryQueryParas = new IndustryQueryParas();
        industryQueryParas.setIndustryId_e(String.valueOf(industryVO.getIndustryId()));
        QueryResults<IndustryPO> industryQueryResults = null;
        try {
            industryQueryResults = getIndustryService().queryIndustry(industryQueryParas);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            throw new BaseUIServiceException(e);
        }
        
        if (!QueryResults.resultsListNotNull(industryQueryResults)) {
            throw new IndustryNotExistException();
        }
        
        IndustryPO oldIndustryPO = industryQueryResults.getResultList().get(0);
        
        oldIndustryPO.setParentId(industryVO.getParentId());
        oldIndustryPO.setCreateTime(industryVO.getCreateTime());
        oldIndustryPO.setName(industryVO.getName());
        oldIndustryPO.setNameEn(industryVO.getNameEn());
        
        try {
            getIndustryService().updateIndustry(oldIndustryPO);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            throw new BaseUIServiceException(e);
        }
    }
    
    @Override
    public List<IndustryVO> getAllIndustries() throws BaseUIServiceException {
    	List<IndustryVO> industries = new ArrayList<IndustryVO>();
    	
    	IndustryQueryParas industryQueryParas = new IndustryQueryParas();
    	QueryResults<IndustryPO> industryQueryResults = null;
    	try {
    		industryQueryResults = getIndustryService().queryIndustry(industryQueryParas);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			throw new BaseUIServiceException(e);
		}
		
		if (QueryResults.resultsListNotNull(industryQueryResults)) {
			for (IndustryPO industryPO : industryQueryResults.getResultList()) {
				if (industryPO != null) {
					industries.add(new IndustryVO(industryPO));
				}
			}
		}
    	
    	return industries;
    }
}